package com.singleton;

import com.inclass.Inclass;
import com.serializable.SerializableSingle;

import java.io.*;
import java.util.concurrent.CountDownLatch;

/**
 * @author wufei
 * @date 2018/3/7/007 21:09
 */
public class ThradSafeTest {

    public static void main(String[] args) {

        CountDownLatch count = new CountDownLatch(100);

        for (int i = 0; i < 100; i++) {

            new Thread(){
                @Override
                public void run() {

//                    Hungry newInstence = Hungry.getNewInstence();
//                    SerializableSingle newInstence = (SerializableSingle) SerializableSingle.getNewInstance(SerializableSingle.class.getName());
                    Inclass newInstence = Inclass.getInstance();
                    System.out.println(System.currentTimeMillis()+"-"+newInstence);
                    try {
                        count.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    count.countDown();
                }
            }.start();
        }

        SerializableSingle s1 = null;
        SerializableSingle s2 = (SerializableSingle) SerializableSingle.getNewInstance(SerializableSingle.class.getName());

        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(s2);
            oos.flush();
            ByteArrayInputStream ios = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream ois = new ObjectInputStream(ios);
            s1 = (SerializableSingle) ois.readObject();
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        boolean b = s1 == s2;
        System.out.printf(b+"");

    }
}
